{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "http://cleverowl.uk/2019/06/02/maths-problem-the-turnip-seller/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting setuptools\n",
      "\u001b[?25l  Downloading https://files.pythonhosted.org/packages/ec/51/f45cea425fd5cb0b0380f5b0f048ebc1da5b417e48d304838c02d6288a1e/setuptools-41.0.1-py2.py3-none-any.whl (575kB)\n",
      "\u001b[K     |████████████████████████████████| 583kB 17.8MB/s eta 0:00:01\n",
      "\u001b[?25hInstalling collected packages: setuptools\n",
      "  Found existing installation: setuptools 40.8.0\n",
      "    Uninstalling setuptools-40.8.0:\n",
      "      Successfully uninstalled setuptools-40.8.0\n",
      "Successfully installed setuptools-41.0.1\n"
     ]
    }
   ],
   "source": [
    "!pip install setuptools --upgrade\n",
    "!pip install -q tensorflow==2.0.0-beta1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.0.0-beta1'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "tf.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "W0716 20:17:54.450118 139782754150208 backprop.py:968] Calling GradientTape.gradient on a persistent tape inside its context is significantly less efficient than calling it outside the context (it causes the gradient ops to be recorded on the tape, leading to increased CPU and memory usage). Only call GradientTape.gradient inside the context if you actually want to trace the gradient in order to compute higher order derivatives.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.49623226840178847\n",
      "0.993561522543563\n",
      "1.491378184724154\n",
      "1.9894857806756314\n",
      "2.487792064612704\n",
      "2.986245078650745\n",
      "3.4848121622206976\n",
      "3.983471209454782\n",
      "4.482206438059112\n",
      "4.981006115793317\n",
      "5.479861240377454\n",
      "5.978764725654101\n",
      "6.477710875284567\n",
      "6.976695028826433\n",
      "7.475713315853073\n",
      "7.974762480357444\n",
      "8.473839752347661\n",
      "8.972942752004473\n",
      "9.472069416845414\n",
      "9.971217945486803\n",
      "10.470386753603055\n",
      "10.969574438998391\n",
      "11.468779753587633\n",
      "11.968001580685925\n",
      "12.467238916427537\n",
      "12.966490854431761\n",
      "13.465756573048296\n",
      "13.965035324670819\n",
      "14.464326426723146\n",
      "14.963629254008728\n",
      "15.46294323217968\n",
      "15.962267832131383\n",
      "16.461602565167265\n",
      "16.960946978808213\n",
      "17.460300653144646\n",
      "17.959663197647714\n",
      "18.459034248370962\n",
      "18.958413465485535\n",
      "19.45780053110163\n",
      "19.957195147336535\n",
      "20.456597034596037\n",
      "20.956005930041037\n",
      "21.45542158621559\n",
      "21.95484376981604\n",
      "22.45427226058395\n",
      "22.9537068503079\n",
      "23.45314734192139\n",
      "23.952593548685755\n",
      "24.45204529344848\n",
      "24.95150240796865\n",
      "25.450964732302168\n",
      "25.95043211424047\n",
      "26.449904408797103\n",
      "26.949381477737298\n",
      "27.448863189146213\n",
      "27.948349417032027\n",
      "28.447840040960507\n",
      "28.94733494571805\n",
      "29.44683402100056\n",
      "29.946337161125715\n",
      "30.445844264766606\n",
      "30.945355234704742\n",
      "31.44486997760079\n",
      "31.944388403781506\n",
      "32.44391042704147\n",
      "32.943435964458395\n",
      "33.44296493622087\n",
      "33.94249726546757\n",
      "34.44203287813702\n",
      "34.94157170282696\n",
      "35.441113670662766\n",
      "35.940658715174045\n",
      "36.44020677217891\n",
      "36.939757779675304\n",
      "37.439311677738864\n",
      "37.938868408426856\n",
      "38.43842791568774\n",
      "38.93799014527599\n",
      "39.43755504467177\n",
      "39.93712256300512\n",
      "40.43669265098441\n",
      "40.936265260828726\n",
      "41.43584034620389\n",
      "41.93541786216197\n",
      "42.434997765083985\n",
      "42.93458001262562\n",
      "43.43416456366574\n",
      "43.933751378257575\n",
      "44.43334041758237\n",
      "44.93293164390534\n",
      "45.43252502053383\n",
      "45.93212051177754\n",
      "46.43171808291064\n",
      "46.93131770013576\n",
      "47.430919330549614\n",
      "47.93052294211034\n",
      "48.430128503606284\n",
      "48.92973598462621\n",
      "49.42934535553096\n",
      "49.92895658742629\n",
      "50.428569652136986\n",
      "50.9281845221821\n",
      "51.42780117075128\n",
      "51.927419571682115\n",
      "52.42703969943845\n",
      "52.92666152908967\n",
      "53.426285036290786\n",
      "53.92591019726341\n",
      "54.4255369887775\n",
      "54.925165388133834\n",
      "55.42479537314724\n",
      "55.92442692213045\n",
      "56.424060013878645\n",
      "56.92369462765458\n",
      "57.4233307431743\n",
      "57.92296834059344\n",
      "58.422607400494016\n",
      "58.92224790387171\n",
      "59.421889832123696\n",
      "59.92153316703688\n",
      "60.421177890776576\n",
      "60.920823985875636\n",
      "61.420471435223924\n",
      "61.92012022205823\n",
      "62.41977032995252\n",
      "62.919421742808524\n",
      "63.41907444484668\n",
      "63.9187284205974\n",
      "64.4183836548926\n",
      "64.9180401328576\n",
      "65.41769783990321\n",
      "65.91735676171818\n",
      "66.41701688426183\n",
      "66.91667819375697\n",
      "67.41634067668302\n",
      "67.91600431976943\n",
      "68.41566910998924\n",
      "68.9153350345529\n",
      "69.41500208090223\n",
      "69.91467023670465\n",
      "70.41433948984758\n",
      "70.91400982843294\n",
      "71.41368124077195\n",
      "71.91335371537997\n",
      "72.41302724097157\n",
      "72.91270180645576\n",
      "73.4123774009313\n",
      "73.91205401368228\n",
      "74.41173163417365\n",
      "74.91141025204705\n",
      "75.41108985711666\n",
      "75.91077043936528\n",
      "76.41045198894038\n",
      "76.91013449615039\n",
      "77.40981795146105\n",
      "77.90950234549184\n",
      "78.4091876690126\n",
      "78.90887391294008\n",
      "79.40856106833485\n",
      "79.90824912639798\n",
      "80.40793807846809\n",
      "80.90762791601834\n",
      "81.40731863065349\n",
      "81.90701021410713\n",
      "82.40670265823891\n",
      "82.9063959550319\n",
      "83.40609009658994\n",
      "83.90578507513519\n",
      "84.4054808830056\n",
      "84.90517751265256\n",
      "85.40487495663858\n",
      "85.90457320763497\n",
      "86.40427225841968\n",
      "86.90397210187513\n",
      "87.40367273098607\n",
      "87.90337413883765\n",
      "88.4030763186133\n",
      "88.90277926359286\n",
      "89.40248296715069\n",
      "89.9021874227538\n",
      "90.40189262396005\n",
      "90.90159856441643\n",
      "91.40130523785731\n",
      "91.90101263810281\n",
      "92.40072075905714\n",
      "92.900429594707\n",
      "93.40013913912011\n",
      "93.89984938644359\n",
      "94.39956033090255\n",
      "94.89927196679862\n",
      "95.39898428850859\n",
      "95.89869729048296\n",
      "96.39841096724466\n",
      "96.89812531338771\n",
      "97.39784032357593\n",
      "97.89755599254174\n",
      "98.39727231508486\n",
      "98.89698928607119\n",
      "99.3967069004316\n",
      "99.89642515316078\n",
      "100.39614403931614\n",
      "100.89586355401676\n",
      "101.39558369244223\n",
      "101.89530444983171\n",
      "102.39502582148283\n",
      "102.89474780275073\n",
      "103.39447038904707\n",
      "103.89419357583911\n",
      "104.39391735864871\n",
      "104.89364173305148\n",
      "105.39336669467585\n",
      "105.89309223920219\n",
      "106.392818362362\n",
      "106.89254505993699\n",
      "107.39227232775836\n",
      "107.89200016170591\n",
      "108.39172855770728\n",
      "108.8914575117372\n",
      "109.39118701981671\n",
      "109.89091707801244\n",
      "110.39064768243584\n",
      "110.89037882924255\n",
      "111.39011051463163\n",
      "111.8898427348449\n",
      "112.3895754861663\n",
      "112.88930876492118\n",
      "113.38904256747574\n",
      "113.88877689023633\n",
      "114.38851172964884\n",
      "114.88824708219815\n",
      "115.38798294440745\n",
      "115.88771931283776\n",
      "116.38745618408728\n",
      "116.88719355479087\n",
      "117.38693142161948\n",
      "117.88666978127964\n",
      "118.3864086305129\n",
      "118.88614796609536\n",
      "119.38588778483711\n",
      "119.88562808358174\n",
      "120.38536885920588\n",
      "120.88511010861873\n",
      "121.38485182876154\n",
      "121.88459401660717\n",
      "122.38433666915964\n",
      "122.88407978345369\n",
      "123.38382335655433\n",
      "123.88356738555645\n",
      "124.38331186758433\n",
      "124.88305679979126\n",
      "125.38280217935917\n",
      "125.88254800349819\n",
      "126.38229426944622\n",
      "126.88204097446865\n",
      "127.3817881158579\n",
      "127.88153569093308\n",
      "128.38128369703958\n",
      "128.8810321315488\n",
      "129.3807809918577\n",
      "129.88053027538854\n",
      "130.38027997958847\n",
      "130.88003010192924\n",
      "131.37978063990684\n",
      "131.87953159104123\n",
      "132.37928295287594\n",
      "132.87903472297782\n",
      "133.3787868989367\n",
      "133.87853947836518\n",
      "134.37829245889816\n",
      "134.87804583819272\n",
      "135.37779961392772\n",
      "135.87755378380356\n",
      "136.3773083455419\n",
      "136.87706329688544\n",
      "137.37681863559752\n",
      "137.87657435946198\n",
      "138.37633046628287\n",
      "138.87608695388414\n",
      "139.37584382010945\n",
      "139.87560106282191\n",
      "140.37535867990383\n",
      "140.87511666925644\n",
      "141.37487502879975\n",
      "141.87463375647224\n",
      "142.3743928502306\n",
      "142.87415230804967\n",
      "143.37391212792204\n",
      "143.87367230785787\n",
      "144.3734328458848\n",
      "144.87319374004753\n",
      "145.37295498840783\n",
      "145.8727165890442\n",
      "146.37247854005165\n",
      "146.87224083954166\n",
      "147.37200348564178\n",
      "147.8717664764956\n",
      "148.37152981026247\n",
      "148.87129348511735\n",
      "149.3710574992506\n",
      "149.87082185086788\n",
      "150.37058653818983\n",
      "150.870351559452\n",
      "151.37011691290468\n",
      "151.86988259681266\n",
      "152.36964860945517\n",
      "152.86941494912557\n",
      "153.36918161413132\n",
      "153.86894860279372\n",
      "154.36871591344786\n",
      "154.8684835444423\n",
      "155.36825149413917\n",
      "155.86801976091374\n",
      "156.36778834315442\n",
      "156.86755723926262\n",
      "157.36732644765257\n",
      "157.86709596675118\n",
      "158.3668657949979\n",
      "158.86663593084458\n",
      "159.36640637275536\n",
      "159.86617711920647\n",
      "160.36594816868617\n",
      "160.86571951969458\n",
      "161.36549117074355\n",
      "161.8652631203566\n",
      "162.36503536706863\n",
      "162.864807909426\n",
      "163.36458074598622\n",
      "163.86435387531802\n",
      "164.36412729600104\n",
      "164.86390100662587\n",
      "165.36367500579382\n",
      "165.86344929211688\n",
      "166.36322386421756\n",
      "166.86299872072883\n",
      "167.36277386029394\n",
      "167.8625492815664\n",
      "168.36232498320976\n",
      "168.86210096389766\n",
      "169.36187722231355\n",
      "169.86165375715072\n",
      "170.36143056711214\n",
      "170.8612076509104\n",
      "171.36098500726752\n",
      "171.86076263491498\n",
      "172.36054053259355\n",
      "172.86031869905318\n",
      "173.36009713305296\n",
      "173.859875833361\n",
      "174.35965479875435\n",
      "174.85943402801885\n",
      "175.35921351994915\n",
      "175.85899327334855\n",
      "176.3587732870289\n",
      "176.85855355981056\n",
      "177.35833409052233\n",
      "177.8581148780013\n",
      "178.3578959210928\n",
      "178.85767721865034\n",
      "179.35745876953553\n",
      "179.85724057261797\n",
      "180.35702262677515\n",
      "180.85680493089245\n",
      "181.35658748386302\n",
      "181.85637028458768\n",
      "182.35615333197492\n",
      "182.85593662494074\n",
      "183.3557201624086\n",
      "183.85550394330943\n",
      "184.35528796658147\n",
      "184.8550722311702\n",
      "185.3548567360283\n",
      "185.85464148011562\n",
      "186.354426462399\n",
      "186.85421168185238\n",
      "187.35399713745653\n",
      "187.85378282819912\n",
      "188.35356875307463\n",
      "188.85335491108427\n",
      "189.35314130123592\n",
      "189.8529279225441\n",
      "190.35271477402983\n",
      "190.85250185472069\n",
      "191.3522891636506\n",
      "191.85207669985994\n",
      "192.35186446239535\n",
      "192.85165245030976\n",
      "193.35144066266227\n",
      "193.85122909851816\n",
      "194.35101775694875\n",
      "194.85080663703144\n",
      "195.35059573784957\n",
      "195.85038505849246\n",
      "196.35017459805522\n",
      "196.84996435563886\n",
      "197.3497543303501\n",
      "197.8495445213014\n",
      "198.3493349276109\n",
      "198.84912554840233\n",
      "199.348916382805\n",
      "199.8487074299537\n",
      "200.34849868898877\n",
      "200.8482901590559\n",
      "201.34808183930616\n",
      "201.847873728896\n",
      "202.34766582698708\n",
      "202.84745813274634\n",
      "203.3472506453459\n",
      "203.847043363963\n",
      "204.34683628778004\n",
      "204.84662941598438\n",
      "205.3464227477685\n",
      "205.84621628232975\n",
      "206.34601001887052\n",
      "206.84580395659796\n",
      "207.34559809472418\n",
      "207.845392432466\n",
      "208.34518696904507\n",
      "208.84498170368772\n",
      "209.34477663562498\n",
      "209.84457176409254\n",
      "210.34436708833064\n",
      "210.84416260758417\n",
      "211.34395832110246\n",
      "211.8437542281394\n",
      "212.3435503279533\n",
      "212.84334661980688\n",
      "213.34314310296728\n",
      "213.84293977670592\n",
      "214.3427366402986\n",
      "214.84253369302533\n",
      "215.34233093417043\n",
      "215.84212836302237\n",
      "216.34192597887377\n",
      "216.84172378102144\n",
      "217.34152176876628\n",
      "217.84131994141322\n",
      "218.34111829827128\n",
      "218.84091683865344\n",
      "219.3407155618767\n",
      "219.84051446726195\n",
      "220.340313554134\n",
      "220.8401128218216\n",
      "221.33991226965725\n",
      "221.8397118969773\n",
      "222.3395117031219\n",
      "222.83931168743499\n",
      "223.33911184926416\n",
      "223.83891218796074\n",
      "224.33871270287975\n",
      "224.8385133933798\n",
      "225.33831425882315\n",
      "225.8381152985756\n",
      "226.33791651200653\n",
      "226.83771789848888\n",
      "227.33751945739905\n",
      "227.83732118811693\n",
      "228.3371230900258\n",
      "228.83692516251247\n",
      "229.33672740496704\n",
      "229.836529816783\n",
      "230.33633239735727\n",
      "230.83613514608996\n",
      "231.33593806238454\n",
      "231.83574114564775\n",
      "232.33554439528953\n",
      "232.8353478107231\n",
      "233.3351513913648\n",
      "233.8349551366342\n",
      "234.33475904595394\n",
      "234.83456311874988\n",
      "235.3343673544509\n",
      "235.834171752489\n",
      "236.33397631229917\n",
      "236.8337810333195\n",
      "237.33358591499106\n",
      "237.8333909567579\n",
      "238.33319615806704\n",
      "238.83300151836843\n",
      "239.33280703711495\n",
      "239.83261271376236\n",
      "240.3324185477693\n",
      "240.83222453859727\n",
      "241.33203068571063\n",
      "241.83183698857653\n",
      "242.33164344666488\n",
      "242.83145005944843\n",
      "243.33125682640264\n",
      "243.83106374700571\n",
      "244.33087082073857\n",
      "244.8306780470848\n",
      "245.33048542553072\n",
      "245.83029295556526\n",
      "246.33010063667996\n",
      "246.82990846836904\n",
      "247.3297164501293\n",
      "247.8295245814601\n",
      "248.32933286186338\n",
      "248.8291412908436\n",
      "249.3289498679078\n",
      "249.82875859256546\n",
      "250.3285674643286\n",
      "250.82837648271166\n",
      "251.32818564723163\n",
      "251.82799495740784\n",
      "252.3278044127621\n",
      "252.8276140128186\n",
      "253.32742375710393\n",
      "253.82723364514703\n",
      "254.32704367647924\n",
      "254.82685385063417\n",
      "255.3266641671478\n",
      "255.82647462555843\n",
      "256.32628522540665\n",
      "256.82609596623524\n",
      "257.32590684758935\n",
      "257.82571786901633\n",
      "258.32552903006575\n",
      "258.82534033028935\n",
      "259.3251517692412\n",
      "259.8249633464774\n",
      "260.3247750615563\n",
      "260.82458691403843\n",
      "261.32439890348644\n",
      "261.82421102946506\n",
      "262.32402329154115\n",
      "262.82383568928367\n",
      "263.3236482222637\n",
      "263.8234608900544\n",
      "264.3232736922308\n",
      "264.8230866283703\n",
      "265.32289969805197\n",
      "265.8227129008572\n",
      "266.3225262363692\n",
      "266.8223397041732\n",
      "267.32215330385645\n",
      "267.82196703500813\n",
      "268.32178089721936\n",
      "268.8215948900832\n",
      "269.3214090131946\n",
      "269.8212232661506\n",
      "270.32103764854986\n",
      "270.82085215999314\n",
      "271.32066680008296\n",
      "271.8204815684238\n",
      "272.3202964646219\n",
      "272.82011148828536\n",
      "273.3199266390241\n",
      "273.81974191644997\n",
      "274.3195573201764\n",
      "274.81937284981876\n",
      "275.31918850499426\n",
      "275.8190042853217\n",
      "276.3188201904217\n",
      "276.8186362199168\n",
      "277.318452373431\n",
      "277.8182686505902\n",
      "278.31808505102197\n",
      "278.8179015743555\n",
      "279.3177182202219\n",
      "279.8175349882536\n",
      "280.3173518780851\n",
      "280.8171688893523\n",
      "281.31698602169274\n",
      "281.8168032747458\n",
      "282.31662064815225\n",
      "282.81643814155467\n",
      "283.31625575459714\n",
      "283.81607348692535\n",
      "284.3158913381866\n",
      "284.8157093080298\n",
      "285.3155273961054\n",
      "285.8153456020653\n",
      "286.3151639255631\n",
      "286.814982366254\n",
      "287.3148009237945\n",
      "287.8146195978428\n",
      "288.31443838805853\n",
      "288.81425729410284\n",
      "289.3140763156384\n",
      "289.8138954523293\n",
      "290.3137147038412\n",
      "290.8135340698412\n",
      "291.31335354999777\n",
      "291.8131731439809\n",
      "292.31299285146207\n",
      "292.8128126721141\n",
      "293.3126326056112\n",
      "293.8124526516292\n",
      "294.31227280984507\n",
      "294.81209307993737\n",
      "295.31191346158596\n",
      "295.8117339544721\n",
      "296.3115545582785\n",
      "296.8113752726891\n",
      "297.3111960973892\n",
      "297.8110170320656\n",
      "298.3108380764063\n",
      "298.81065923010067\n",
      "299.3104804928394\n",
      "299.81030186431457\n",
      "300.31012334421945\n",
      "300.8099449322487\n",
      "301.3097666280982\n",
      "301.8095884314652\n",
      "302.3094103420482\n",
      "302.8092323595469\n",
      "303.3090544836624\n",
      "303.8088767140969\n",
      "304.308699050554\n",
      "304.8085214927384\n",
      "305.30834404035613\n",
      "305.80816669311446\n",
      "306.3079894507218\n",
      "306.8078123128878\n",
      "307.30763527932334\n",
      "307.80745834974056\n",
      "308.3072815238526\n",
      "308.80710480137407\n",
      "309.3069281820205\n",
      "309.80675166550867\n",
      "310.3065752515566\n",
      "310.80639893988337\n",
      "311.30622273020936\n",
      "311.8060466222559\n",
      "312.3058706157456\n",
      "312.8056947104022\n",
      "313.3055189059504\n",
      "313.80534320211626\n",
      "314.3051675986268\n",
      "314.80499209521014\n",
      "315.30481669159565\n",
      "315.80464138751364\n",
      "316.30446618269553\n",
      "316.80429107687394\n",
      "317.3041160697824\n",
      "317.8039411611557\n",
      "318.3037663507295\n",
      "318.8035916382407\n",
      "319.3034170234271\n",
      "319.80324250602763\n",
      "320.3030680857823\n",
      "320.802893762432\n",
      "321.30271953571884\n",
      "321.80254540538584\n",
      "322.3023713711771\n",
      "322.8021974328377\n",
      "323.3020235901137\n",
      "323.80184984275223\n",
      "324.3016761905013\n",
      "324.8015026331101\n",
      "325.30132917032864\n",
      "325.801155801908\n",
      "326.3009825276002\n",
      "326.8008093471582\n",
      "327.300636260336\n",
      "327.8004632668885\n",
      "328.3002903665717\n",
      "328.8001175591423\n",
      "329.2999448443581\n",
      "329.79977222197783\n",
      "330.2995996917611\n",
      "330.79942725346854\n",
      "331.2992549068616\n",
      "331.7990826517028\n",
      "332.29891048775545\n",
      "332.79873841478377\n",
      "333.2985664325529\n",
      "333.7983945408289\n",
      "334.29822273937873\n",
      "334.79805102797025\n",
      "335.2978794063722\n",
      "335.7977078743542\n",
      "336.2975364316867\n",
      "336.79736507814107\n",
      "337.29719381348957\n",
      "337.7970226375053\n",
      "338.2968515499621\n",
      "338.7966805506349\n",
      "339.2965096392993\n",
      "339.7963388157318\n",
      "340.29616807970984\n",
      "340.7959974310115\n",
      "341.2958268694158\n",
      "341.79565639470263\n",
      "342.2954860066526\n",
      "342.79531570504724\n",
      "343.2951454896688\n",
      "343.79497536030044\n",
      "344.29480531672607\n",
      "344.79463535873043\n",
      "345.294465486099\n",
      "345.79429569861816\n",
      "346.29412599607497\n",
      "346.7939563782573\n",
      "347.29378684495384\n",
      "347.79361739595413\n",
      "348.2934480310483\n",
      "348.7932787500274\n",
      "349.29310955268323\n",
      "349.7929404388083\n",
      "350.2927714081958\n",
      "350.7926024606399\n",
      "351.2924335959354\n",
      "351.79226481387775\n",
      "352.29209611426336\n",
      "352.7919274968892\n",
      "353.291758961553\n",
      "353.79159050805333\n",
      "354.2914221361894\n",
      "354.79125384576116\n",
      "355.29108563656933\n",
      "355.79091750841525\n",
      "356.2907494611011\n",
      "356.79058149442966\n",
      "357.29041360820446\n",
      "357.7902458022298\n",
      "358.2900780763105\n",
      "358.7899104302523\n",
      "359.2897428638615\n",
      "359.7895753769451\n",
      "360.28940796931084\n",
      "360.7892406407671\n",
      "361.28907339112294\n",
      "361.78890622018815\n",
      "362.2887391277731\n",
      "362.788572113689\n",
      "363.2884051777475\n",
      "363.7882383197611\n",
      "364.2880715395428\n",
      "364.7879048369064\n",
      "365.28773821166635\n",
      "365.78757166363766\n",
      "366.287405192636\n",
      "366.7872387984778\n",
      "367.28707248098\n",
      "367.78690623996016\n",
      "368.28674007523665\n",
      "368.78657398662835\n",
      "369.28640797395474\n",
      "369.786242037036\n",
      "370.2860761756929\n",
      "370.7859103897469\n",
      "371.28574467901996\n",
      "371.78557904333474\n",
      "372.28541348251446\n",
      "372.78524799638296\n",
      "373.2850825847648\n",
      "373.7849172474849\n",
      "374.284751984369\n",
      "374.7845867952434\n",
      "375.2844216799349\n",
      "375.78425663827096\n",
      "376.28409167007965\n",
      "376.78392677518957\n",
      "377.28376195343\n",
      "377.7835972046306\n",
      "378.28343252862186\n",
      "378.78326792523467\n",
      "379.28310339430055\n",
      "379.7829389356516\n",
      "380.28277454912046\n",
      "380.7826102345404\n",
      "381.2824459917452\n",
      "381.78228182056915\n",
      "382.2821177208472\n",
      "382.78195369241485\n",
      "383.281789735108\n",
      "383.78162584876327\n",
      "384.28146203321774\n",
      "384.7812982883091\n",
      "385.2811346138755\n",
      "385.7809710097557\n",
      "386.280807475789\n",
      "386.7806440118152\n",
      "387.2804806176746\n",
      "387.7803172932081\n",
      "388.2801540382571\n",
      "388.77999085266345\n",
      "389.2798277362697\n",
      "389.77966468891873\n",
      "390.2795017104541\n",
      "390.77933880071976\n",
      "391.2791759595602\n",
      "391.77901318682046\n",
      "392.2788504823461\n",
      "392.77868784598314\n",
      "393.27852527757807\n",
      "393.778362776978\n",
      "394.27820034403044\n",
      "394.7780379785834\n",
      "395.2778756804855\n",
      "395.77771344958575\n",
      "396.27755128573364\n",
      "396.7773891887792\n",
      "397.2772271585729\n",
      "397.7770651949657\n",
      "398.2769032978091\n",
      "398.776741466955\n",
      "399.2765797022559\n",
      "399.77641800356463\n",
      "400.27625637073453\n",
      "400.6761270932084\n",
      "400.9860269193486\n",
      "401.2149529236879\n",
      "401.37100247421057\n",
      "401.46146320291575\n",
      "401.49289397933404\n",
      "401.4711977867008\n",
      "401.40168731051966\n",
      "401.2891439682762\n",
      "401.13787103618694\n",
      "400.951741463278\n",
      "400.7342409040616\n",
      "400.48850644794896\n",
      "400.2173614757258\n",
      "399.92334703038375\n",
      "399.70871795498357\n",
      "399.56553572612376\n",
      "399.4866556719738\n",
      "399.4656475871944\n",
      "399.49672428635813\n",
      "399.5746773020207\n",
      "399.6948190129785\n",
      "399.8529305596949\n",
      "400.04521496817887\n",
      "400.16828694415875\n",
      "400.229067718515\n",
      "400.2337863996957\n",
      "400.18804918589393\n",
      "400.0969016560141\n",
      "399.9648848315478\n",
      "399.89605372382727\n",
      "399.88408977283\n",
      "399.9233062739459\n",
      "400.0085851924952\n",
      "400.0353521562611\n",
      "400.00945834959464\n",
      "399.9361698389529\n",
      "399.920194261612\n",
      "399.9558003351841\n",
      "400.03782990499417\n",
      "400.0616724181664\n",
      "400.0331465694253\n",
      "399.9574891845524\n",
      "399.939381656408\n",
      "399.9730690101251\n",
      "400.0533717678039\n",
      "400.07566011393016\n",
      "400.04573547889106\n",
      "399.9688191505543\n",
      "399.9495786089348\n",
      "399.9822462860232\n",
      "400.06163137009895\n",
      "400.0830937743816\n",
      "400.0524257562436\n",
      "399.97484034783014\n",
      "399.95499766938343\n",
      "399.987123458423\n",
      "400.0660208782124\n",
      "400.08704434952153\n",
      "400.0559812567462\n",
      "399.9780402463437\n",
      "399.9578775609317\n",
      "399.9897153783813\n",
      "400.0683536582803\n",
      "400.0891438690315\n",
      "400.05787080724735\n",
      "399.97974079037186\n",
      "399.9594080335371\n",
      "399.99109282096435\n",
      "400.06959340796453\n",
      "400.09025966089234\n",
      "400.05887500299895\n",
      "399.98064451569496\n",
      "399.9602213694568\n",
      "399.9918248402602\n",
      "400.070252276003\n",
      "400.0908526590143\n",
      "400.0594086845543\n",
      "399.9811247788339\n",
      "399.9606535895864\n",
      "399.9922138551061\n",
      "400.0706024393851\n",
      "400.0911678227137\n",
      "400.05969231531446\n",
      "399.9813799968564\n",
      "399.96088326929936\n",
      "399.99242058335625\n",
      "400.07078854420354\n",
      "400.0913353334893\n",
      "400.05984305863495\n",
      "399.9815156167809\n",
      "399.961005310917\n",
      "399.99253043711093\n",
      "400.07088746136594\n",
      "400.09142437516755\n",
      "400.0599231799616\n",
      "399.98158767750306\n",
      "399.9610701494463\n",
      "399.99258880788375\n",
      "400.07094004324847\n",
      "400.09147171489354\n",
      "400.05996576972433\n",
      "399.98162596040214\n",
      "399.96110458812745\n",
      "399.99261981859644\n",
      "400.0709680004928\n",
      "400.09149689224995\n",
      "400.05998841354574\n",
      "399.98164629252966\n",
      "399.9611228713047\n",
      "399.9926362891633\n",
      "400.07098287103264\n",
      "400.0915102913811\n",
      "400.06000045715314\n",
      "399.9816570850311\n",
      "399.96113256900634\n",
      "399.9926450326133\n",
      "400.07099078660434\n",
      "400.0915174308535\n",
      "400.06000686725366\n",
      "399.9816628079336\n",
      "399.961137704254\n",
      "399.99264966966973\n",
      "400.07099500586844\n",
      "400.09152124346497\n",
      "400.0600102833623\n",
      "399.98166583679165\n",
      "399.96114041504364\n",
      "399.992652124532\n",
      "400.070997260614\n",
      "400.0915232878289\n",
      "400.0600121082283\n",
      "399.9816674340703\n",
      "399.96114183759096\n",
      "399.9926534197974\n",
      "400.0709984711876\n",
      "400.0915243922603\n",
      "400.0600130873321\n",
      "399.9816682706931\n",
      "399.9611425757242\n",
      "399.99265409891433\n",
      "400.0709991267017\n",
      "400.0915249969633\n",
      "400.06001361685463\n",
      "399.9816687032142\n",
      "399.96114295033925\n",
      "399.9926544506921\n",
      "400.0709994870934\n",
      "400.0915253358843\n",
      "400.06001390734474\n",
      "399.9816689211191\n",
      "399.9611431319702\n",
      "399.9926546286141\n",
      "400.07099969050614\n",
      "400.0915255333551\n",
      "400.0600140706983\n",
      "399.98166902510553\n",
      "399.9611432112422\n",
      "399.9926547142458\n",
      "400.07099981035725\n",
      "400.091525655454\n",
      "400.06001416638304\n",
      "399.9816690686865\n",
      "399.9611432363143\n",
      "399.99265475093307\n",
      "400.07099988566597\n",
      "400.09152573730114\n",
      "400.06001422600434\n",
      "399.9816690802986\n",
      "399.96114323277675\n",
      "399.9926547617098\n",
      "400.07099993717077\n",
      "400.0915257975637\n",
      "400.0600142663601\n",
      "399.9816690750523\n",
      "399.9611432142264\n",
      "399.99265475881566\n",
      "400.0709999758952\n",
      "400.09152584616544\n",
      "400.0600142963792\n",
      "399.98166906097595\n",
      "399.9611431878862\n",
      "399.9926547487539\n",
      "400.0710000076996\n",
      "400.0915258883832\n",
      "400.0600143208082\n",
      "399.98166904233375\n",
      "399.96114315759144\n",
      "399.99265473497877\n",
      "400.07100003570025\n",
      "400.0915259270242\n",
      "400.06001434217114\n",
      "399.98166902139025\n",
      "399.96114312537776\n",
      "399.9926547193248\n",
      "400.0710000615555\n",
      "400.09152596358314\n",
      "400.060014361811\n",
      "399.9816689993468\n"
     ]
    }
   ],
   "source": [
    "# Import `tensorflow` \n",
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "@tf.function\n",
    "def step(x):\n",
    "    return x-(6/7)*x-1/7\n",
    "\n",
    "#print(tf.autograph.to_code(step.python_function))\n",
    "\n",
    "\n",
    "w = tf.Variable(0, trainable=True, dtype=tf.float64)\n",
    "y = tf.constant([0], dtype=tf.float64)\n",
    "\n",
    "@tf.function\n",
    "def compute(x):\n",
    "    return step(step(step(step(x))))\n",
    "\n",
    "#print(tf.autograph.to_code(compute.python_function))\n",
    "\n",
    "\n",
    "variables = [w]\n",
    "optimizer = tf.optimizers.Adam(0.5)\n",
    "\n",
    "loss_object = tf.keras.losses.MeanAbsoluteError()\n",
    "\n",
    "with tf.GradientTape(persistent=True) as tape:\n",
    "    for i in range(1000):\n",
    "        y_pred = compute(w)\n",
    "        loss = loss_object(y,y_pred)\n",
    "        grads = tape.gradient(loss, variables)\n",
    "        optimizer.apply_gradients(zip(grads, variables))\n",
    "        print(w.numpy())\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
